﻿package com.actionpals.components.controls.calendar
{
	import com.actionpals.events.CalendarEvent;
	import com.actionpals.graphics.RectangleShape;
	import com.actionpals.utils.DateUtil;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
    public class CalendarDay extends Sprite
    {
    	private var bg:RectangleShape;
    	private var text:TextField;
    	
		private var events:Array;
		private var style:CalendarStyle;
		
		private var _date:Date;
		private var _width:Number;
		private var _height:Number;
		private var _showToday:Boolean;
		private var _isToday:Boolean;
		private var _selected:Boolean;
		private var _visited:Boolean;
		
		public function CalendarDay( date:Date, width:Number, height:Number, showToday:Boolean, events:Array = null, style:CalendarStyle = null )
		{
			_date = date;
			_width = width;
			_height = height;
			_showToday = showToday;
			this.events = events;
			this.style = style;
			
			init();
		}
		
		public function get date():Date
		{
			return _date;
		}
		
		public function get isToday():Boolean
		{
			return _isToday;
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		public function set selected( value:Boolean ):void
		{
			_selected = value;
		}
		
		public function get visited():Boolean
		{
			return _visited;
		}
		public function set visited( value:Boolean ):void
		{
			_visited = value;
		}
		
		public function drawNow():void
		{
			draw();
		}
		
		private function init():void
		{
			this.addEventListener( MouseEvent.ROLL_OVER, onRollOver );
			this.addEventListener( MouseEvent.ROLL_OUT, onRollOut );
			this.addEventListener( MouseEvent.CLICK, onClick );
			
			create();
			draw();
		}
		
		private function create():void
		{
			bg = new RectangleShape( 20, 20 );
			text = new TextField();
			addChild( bg );
			addChild( text );
			
			text.text = String( date.date );
			text.autoSize = TextFieldAutoSize.CENTER;
			text.selectable = false;
		}
		
		private function draw( rollOver:Boolean=false ):void
		{
			var stateStyle:CalendarDateStateStyle;
			_isToday = DateUtil.compareDates( new Date(), date ) == 0;
			
			if( selected ) stateStyle = drawStateStyle( "selected" );
			else
			{
				if( visited )
				{
					if( rollOver ) stateStyle = drawStateStyle( "visitedOver" );
					else stateStyle = drawStateStyle( "visitedUp" );
				}
				else
				{
					if( rollOver ) stateStyle = drawStateStyle( "over" );
					else stateStyle = drawStateStyle( "up" );
				}
			}
			
			bg.width = _width;
			bg.height = _height;
			bg.fillColor = stateStyle.color;
			bg.fillAlpha = stateStyle.alpha;
			bg.strokeColor = stateStyle.borderColor;
			bg.strokeAlpha = stateStyle.borderAlpha;
			bg.strokeThickness = stateStyle.borderThickness;
			bg.drawNow();
			
			text.x = bg.width / 2 - text.width / 2;
			text.y = bg.height / 2 - text.textHeight / 2;
			text.setTextFormat( stateStyle.textFormat );
			text.embedFonts = stateStyle.embedFonts;
		}
		
		private function drawStateStyle( state:String ):CalendarDateStateStyle
		{
			var bgColor:uint = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].color : style.todayStyle[ state + "Style" ].color : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].color : style.dayStyle[ state + "Style" ].color;
			var bgAlpha:Number = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].alpha : style.todayStyle[ state + "Style" ].alpha : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].alpha : style.dayStyle[ state + "Style" ].alpha;
			var borderColor:uint = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].borderColor : style.todayStyle[ state + "Style" ].borderColor : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].borderColor : style.dayStyle[ state + "Style" ].borderColor;
			var borderAlpha:Number = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].borderAlpha : style.todayStyle[ state + "Style" ].borderAlpha : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].borderAlpha : style.dayStyle[ state + "Style" ].borderAlpha;
			var borderThickness:Number = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].borderThickness : style.todayStyle[ state + "Style" ].borderThickness : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].borderThickness : style.dayStyle[ state + "Style" ].borderThickness;
			var textFormat:TextFormat = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].textFormat : style.todayStyle[ state + "Style" ].textFormat : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].textFormat : style.dayStyle[ state + "Style" ].textFormat;
			var embedFonts:Boolean = isToday && _showToday ? ( events.length > 0 ) ? style.todayEventStyle[ state + "Style" ].embedFonts : style.todayStyle[ state + "Style" ].embedFonts : ( events.length > 0 ) ? style.eventStyle[ state + "Style" ].embedFonts : style.dayStyle[ state + "Style" ].embedFonts;
			
			return new CalendarDateStateStyle( textFormat, embedFonts, bgColor, bgAlpha, borderColor, borderAlpha, borderThickness );
		}
		
		
		private function onRollOver( evt:MouseEvent ):void
		{
			draw( true );
		}
			
		private function onRollOut( evt:MouseEvent ):void
		{
			draw();
		}
			
		private function onClick( evt:MouseEvent ):void
		{
			dispatchEvent( new CalendarEvent( CalendarEvent.DATE_SELECT, date, true, true, events ) );
		}
		
	}
}